using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

using CNCSoft.EData;

using Common;

namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuChiData.
	/// </summary>
	public class TienMatPhieuChiData: TMSDataBase {

		public enum TL  {
			TienMatPC,
			TienMatPC_CT,
			DoiTac,
			LyDoChi,
			LyDoChi_Detail
		}

		private string sqlslPTTM_CT = 
			"SELECT [ID], SoChungtu, TaiKhoanNo, MaTieuKhoanNo1, " +
			"		MaTieuKhoanNo2, TaiKhoanCo, MaTieuKhoanCo1, MaTieuKhoanCo2, " +
			"		DienGiaiChiTiet, SoTien " +
			"FROM   TIENMAT_CHITIET TMCT ";

		public TienMatPhieuChiData() {

			vg.TableName = "TIENMAT";
			vg.FieldName = "SoChungTu";
			vg.Prefix = WSInfo["TienMat.PhieuChi.MaLoaiCT"];

			ds.Tables.Add("TienMatPC");
			ds.Tables.Add("TienMatPC_CT");
			ds.Tables.Add("DoiTac");
			ds.Tables.Add("LyDoChi");
			ds.Tables.Add("LyDoChi_Detail");

			DanhMuc.fill(getTable(TL.DoiTac), DanhMuc.TLDM.DoiTac);
			DanhMuc.fill(getTable(TL.LyDoChi), DanhMuc.TLDM.LyDoChi);
			DanhMuc.fill(getTable(TL.LyDoChi_Detail), DanhMuc.TLDM.LyDoChi);

			initData("('')", true);
		}

		public void initData(string soChungTus, bool firstLoading) {
			if (!firstLoading) {
				getTable(TL.TienMatPC_CT).Clear();
				getTable(TL.TienMatPC).Clear();
			}

			string sqlslPTTM = 
				"SELECT	TM.[SoChungTu], TM.[MaLoaiChungtu], TM.[NgayChungtu], TM.[MaDonVi], TM.[MaDoitac], " +
                "		TM.[Hoten], TM.[DienGiai], TM.[SoChungtuKemTheo], [SoPhieuYeuCau], [ThanhToanHetPYC],  TM.[Diachi], " +
				"		TM.[TongTien], TM.[UpdateUserId], TM.[UpdateDate], TM.[LyDoThuChi], " +
                "		TM.MaSoThue, TM.CoThue, SoHoaDon, SerialHoaDon, NgayHoaDon, ThueSuat " +
				"FROM 	[TIENMAT] TM Inner Join DM_DoiTac DT on TM.MaDoiTac = DT.MaDoiTac ";

            try
            {
                SQLHelper.Fill(getTable(TL.TienMatPC), GlobalVariable.WSInfo.ConnectionString,
                    sqlslPTTM + "WHERE [SoChungTu] In " + soChungTus);

                SQLHelper.Fill(getTable(TL.TienMatPC_CT), GlobalVariable.WSInfo.ConnectionString,
                    sqlslPTTM_CT + "WHERE TMCT.SoChungTu In " + soChungTus);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }

			if (firstLoading) {
				// Set primary key(s)
				getTable(TL.TienMatPC).PrimaryKey = new DataColumn[]{getTable(TL.TienMatPC).Columns["SoChungTu"]};
				getTable(TL.TienMatPC_CT).PrimaryKey = new DataColumn[]{getTable(TL.TienMatPC_CT).Columns["ID"]};

				// Set relations
				ds.Relations.Add("M_D", getTable(TL.TienMatPC).Columns["SoChungTu"],
					getTable(TL.TienMatPC_CT).Columns["SoChungTu"]);

				// Set default values
				getTable(TL.TienMatPC).Columns["SoChungTu"].DefaultValue = "";
				getTable(TL.TienMatPC).Columns["NgayChungTu"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                getTable(TL.TienMatPC).Columns["MaDonVi"].DefaultValue = GlobalVariable.WSInfo.CompanyID;
				getTable(TL.TienMatPC).Columns["UpdateUserId"].DefaultValue = GlobalVariable.WSInfo.UserID;
				getTable(TL.TienMatPC).Columns["UpdateDate"].DefaultValue = GlobalVariable.WSInfo.ServerDate;
				getTable(TL.TienMatPC).Columns["TongTien"].DefaultValue = 0;
				getTable(TL.TienMatPC).Columns["MaLoaiChungTu"].DefaultValue = GlobalVariable.WSInfo["TienMat.PhieuChi.MaLoaiCT", "PC"];
                getTable(TL.TienMatPC).Columns["SoPhieuYeuCau"].DefaultValue = "";
                getTable(TL.TienMatPC).Columns["ThanhToanHetPYC"].DefaultValue = 0;
				getTable(TL.TienMatPC).Columns["CoThue"].DefaultValue = 0;
                getTable(TL.TienMatPC).Columns["ThueSuat"].DefaultValue = 0;
                getTable(TL.TienMatPC).Columns["SoHoaDon"].DefaultValue = "";
                getTable(TL.TienMatPC).Columns["SerialHoaDon"].DefaultValue = "";

				getTable(TL.TienMatPC_CT).Columns["ID"].AutoIncrement = true;
				getTable(TL.TienMatPC_CT).Columns["ID"].AutoIncrementStep = 1;
				getTable(TL.TienMatPC_CT).Columns["SoTien"].DefaultValue = 0;
				getTable(TL.TienMatPC_CT).Columns["TaiKhoanCo"].DefaultValue = GlobalVariable.WSInfo["TienMat.PhieuChi.MaTKC", "1111"];
                //Don vi nao chi? (Tan Binh, Tan Phu, Online, ...)
                getTable(TL.TienMatPC_CT).Columns["MaTieuKhoanCo1"].DefaultValue = GlobalVariable.WSInfo["Sys.MaDonVi", "?"];
			}
		}

		public bool createNewRow() {
			try {
				DataRow dr = getTable(TL.TienMatPC).NewRow();
                vg.DateForGenerating = GlobalVariable.WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "PC";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
				dr["SoChungTu"] = vg.generateVoucherNo();
				getTable(TL.TienMatPC).Rows.Add(dr);

				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
				new SimpleMasterDetailUpdater(
					"SELECT	[SoChungTu], [MaLoaiChungtu], [NgayChungtu], [MaDonVi], [MaDoitac], " +
                    "		[Hoten], [DienGiai], [SoChungtuKemTheo], [SoPhieuYeuCau], [ThanhToanHetPYC], [Diachi], " +
					"		[TongTien], [UpdateUserId], [UpdateDate], [LyDoThuChi], " +
					"       [Masothue], [CoThue], [ThueSuat], [SoHoaDon], [SerialHoaDon], [NgayHoaDon] " +
					"FROM 	[TIENMAT] ", sqlslPTTM_CT, getTable(TL.TienMatPC), getTable(TL.TienMatPC_CT), GlobalVariable.WSInfo.ConnectionString);
				ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}
	}
}
